package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.StudyAbroadDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.StudyAbroad;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLStudyAbroadInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.StudyAbroadInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddStudyAbroadMutation implements DataFetcher {

    @Autowired
    StudyAbroadDao studyAbroadDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加留学情况
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLStudyAbroadInput input = StudyAbroadInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        StudyAbroad studyAbroad = null;

        if (input.getId() != null) {
            studyAbroad = studyAbroadDao.get(StudyAbroad.class, input.getId());
            studyAbroad.setUpdatedAt(new Date());
        } else {
            studyAbroad = new StudyAbroad();
            studyAbroad.setCreatedAt(new Date());
        }
        if(schoolId != null){

            studyAbroad.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            studyAbroad.setTeacher_id(input.getTeacher_id());
        }
        if(input.getCountry() != null){

            studyAbroad.setCountry(input.getCountry());
        }
        if(input.getPub() != null){

            studyAbroad.setPub(input.getPub());
        }
        if(input.getTime() != null){

            studyAbroad.setTime(input.getTime());
        }
        if(input.getEntime() != null){

            studyAbroad.setEntime(input.getEntime());
        }
        if(input.getBack() != null){

            studyAbroad.setBack(input.getBack());
        }
        if(input.getProjectdate() != null){

            studyAbroad.setProjectdate(input.getProjectdate());
        }
        if(input.getProjectmoney() != null){

            studyAbroad.setProjectmoney(input.getProjectmoney());
        }
        if(input.getImage() != null){

            studyAbroad.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            studyAbroad.setShenhe(input.getShenhe());
        }
        if(input.getName() != null){

            studyAbroad.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getEducational() != null){

            studyAbroad.setEducational(JSON.toJSONString(input.getEducational()));
        }
        if(input.getProject() != null){

            studyAbroad.setProject(JSON.toJSONString(input.getProject()));
        }
        try{
            studyAbroadDao.saveOrUpdate(studyAbroad);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

